﻿using OnLineLogFileComm;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace OnLineLogFileClient
{
    public class UpdateJob : SocketTcpJobAsync
    {
        public override void Run()
        {
            this.TryConnect();
            if (this.HasConnected)
            {
                UpdateClient();
            }
        }
        private static string GetAssemblyPath()
        {
            string l = System.Reflection.Assembly.GetEntryAssembly().Location;

            FileInfo fi = new FileInfo(l);
            return fi.DirectoryName;
        }

        private void UpdateClient()
        {
            this.InvMsg("检查更新。。。");
            Command cmd = new Command();
            cmd.CommandType = OnLineLogFileComm.CommandType.CheckUpdateClient;
            cmd.Data = this.GetType().Assembly.GetName().Version.ToJson();
            client.Send(cmd);
            var rData = client.Receive();
            var dyD = rData.GetObjData<dynamic>();
            if ((bool)dyD.status)
            {

                Process.Start(Path.Combine(GetAssemblyPath(), "OnLineLogFileClient.Update.exe"));
                Environment.Exit(0);
            }
        }
    }
}
